Skip to contentMethod: persistAxioms(NamedResource, Map)
1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.ontodriver.owlapi;
16:
17: import cz.cvut.kbss.ontodriver.owlapi.connector.OntologySnapshot;
18: import cz.cvut.kbss.ontodriver.owlapi.util.MutableAddAxiom;
19: import cz.cvut.kbss.ontodriver.descriptor.AxiomValueDescriptor;
20: import cz.cvut.kbss.ontodriver.model.Assertion;
21: import cz.cvut.kbss.ontodriver.model.AxiomImpl;
22: import cz.cvut.kbss.ontodriver.model.NamedResource;
23: import cz.cvut.kbss.ontodriver.model.Value;
24: import cz.cvut.kbss.ontodriver.owlapi.util.OwlapiUtils;
25: import org.semanticweb.owlapi.model.*;
26:
27: import java.net.URI;
28: import java.util.*;
29: import java.util.stream.Collectors;
30:
31: /**
32: * Persists axioms into the ontology snapshot.
33: */
34: class AxiomSaver {
35:
36: private final OwlapiAdapter adapter;
37:
38: private final OWLOntology ontology;
39: private final OntologySnapshot snapshot;
40:
41: private final AxiomAdapter axiomAdapter;
42:
43: AxiomSaver(OwlapiAdapter adapter, OntologySnapshot snapshot) {
44: this.adapter = adapter;
45: this.snapshot = snapshot;
46: this.ontology = snapshot.getOntology();
47: this.axiomAdapter = new AxiomAdapter(snapshot.getDataFactory(), adapter.getLanguage());
48: }
49:
50: void persist(AxiomValueDescriptor descriptor) {
51: for (Assertion assertion : descriptor.getAssertions()) {
52: switch (assertion.getType()) {
53: case CLASS:
54: persistTypes(descriptor.getSubject(), descriptor.getAssertionValues(assertion));
55: break;
56: case DATA_PROPERTY:
57: persistDataPropertyValues(descriptor.getSubject(), assertion,
58: descriptor.getAssertionValues(assertion));
59: break;
60: case ANNOTATION_PROPERTY:
61: persistAnnotationPropertyValues(descriptor.getSubject(), assertion,
62: descriptor.getAssertionValues(assertion));
63: break;
64: case OBJECT_PROPERTY:
65: persistObjectPropertyValues(descriptor.getSubject(), assertion,
66: descriptor.getAssertionValues(assertion));
67: break;
68: case PROPERTY:
69: persistPropertyValues(descriptor.getSubject(), assertion, descriptor.getAssertionValues(assertion));
70: break;
71: }
72: }
73: }
74:
75: private void persistTypes(NamedResource subject, List<Value<?>> types) {
76: final Set<URI> classes = types.stream().map(val -> {
77: if (val.getValue() instanceof URI) {
78: return (URI) val.getValue();
79: } else {
80: return URI.create(val.stringValue());
81: }
82: }).collect(Collectors.toSet());
83: adapter.getTypesHandler().addTypes(subject, null, classes);
84: }
85:
86: private void persistDataPropertyValues(NamedResource subject, Assertion assertion, Collection<Value<?>> values) {
87: final List<OWLAxiom> axioms = values.stream().filter(value -> value != Value.nullValue())
88: .map(value -> axiomAdapter.toOwlDataPropertyAssertionAxiom(
89: new AxiomImpl<>(subject, assertion, value)))
90: .collect(Collectors.toList());
91: addAxioms(axioms);
92: }
93:
94: private void addAxioms(List<? extends OWLAxiom> axioms) {
95: if (axioms.isEmpty()) {
96: return;
97: }
98: final List<OWLOntologyChange> changes = axioms.stream().map(axiom -> new MutableAddAxiom(ontology, axiom))
99: .collect(Collectors.toList());
100: adapter.addTransactionalChanges(snapshot.applyChanges(changes));
101: }
102:
103: private void persistAnnotationPropertyValues(NamedResource subject, Assertion assertion,
104: Collection<Value<?>> values) {
105: final List<OWLAxiom> axioms = values.stream().filter(value -> value != Value.nullValue())
106: .map(value -> axiomAdapter.toOwlAnnotationPropertyAssertionAxiom(
107: new AxiomImpl<>(subject, assertion, value)))
108: .collect(Collectors.toList());
109: addAxioms(axioms);
110: }
111:
112: private void persistObjectPropertyValues(NamedResource subject, Assertion assertion, Collection<Value<?>> values) {
113: final List<OWLAxiom> axioms = values.stream().filter(value -> value != Value.nullValue())
114: .map(value -> {
115: // Simplistic version using value.stringValue
116: // We expect the value to be a NamedResource, but in case the property was unspecified and it was only assumed
117: // it is an object property (see #persistPropertyValues), the value would be a simple string
118: return axiomAdapter.toOwlObjectPropertyAssertionAxiom(
119: new AxiomImpl<>(subject, assertion, value));
120: }).collect(Collectors.toList());
121: addAxioms(axioms);
122: }
123:
124: private void persistPropertyValues(NamedResource subject, Assertion assertion, Collection<Value<?>> values) {
125: final IRI property = IRI.create(assertion.getIdentifier());
126: if (ontology.containsDataPropertyInSignature(property)) {
127: persistDataPropertyValues(subject, assertion, values);
128: } else if (ontology.containsObjectPropertyInSignature(property)) {
129: persistObjectPropertyValues(subject, assertion, values);
130: } else if (ontology.containsAnnotationPropertyInSignature(property)) {
131: persistAnnotationPropertyValues(subject, assertion, values);
132: } else {
133: persistUnknownPropertyValues(subject, assertion, values);
134: }
135: }
136:
137: private void persistUnknownPropertyValues(NamedResource subject, Assertion assertion, Collection<Value<?>> values) {
138: final List<OWLAxiom> axioms = new ArrayList<>();
139: for (Value<?> v : values) {
140: if (OwlapiUtils.isIndividualIri(v.getValue())) {
141: axioms.add(axiomAdapter.toOwlObjectPropertyAssertionAxiom(new AxiomImpl<>(subject, assertion, v)));
142: } else {
143: axioms.add(axiomAdapter.toOwlDataPropertyAssertionAxiom(new AxiomImpl<>(subject, assertion, v)));
144: }
145: }
146: addAxioms(axioms);
147: }
148:
149: void persistAxioms(NamedResource subject, Map<Assertion, Set<Value<?>>> properties) {
150:• for (Map.Entry<Assertion, Set<Value<?>>> e : properties.entrySet()) {
151: persistPropertyValues(subject, e.getKey(), e.getValue());
152: }
153: }
154: }